package com.cal.calonecal.utils;

import cn.hutool.core.util.RandomUtil;
import com.cal.calonecal.pojo.vo.EquationVO;
import org.nfunk.jep.JEP;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 76004
 * @apiNote
 * @date 2023/5/10 17:03
 */
@Component
public class EquationBuilder {

    /***
     * 生成左项和右项结果均不超过20的加减法
     * @return
     */
    public EquationVO createLel1(){
        EquationVO equationEntity = new EquationVO();
        String equation = null;
        boolean isAdd = RandomUtil.randomBoolean();
        if(isAdd){
            //加法
            Long ans = RandomUtil.randomLong(1,21);
            Long left = RandomUtil.randomLong(ans);
            equation = ""+left+"+"+(ans-left);
        }else{
            Long left = RandomUtil.randomLong(1,21);
            Long ans = RandomUtil.randomLong(left);
            equation = ""+left+"-"+(left-ans);
        }
        equationEntity.setEquation(equation);
        equationEntity.setLevel(1);
        equationEntity.setType(1);
        return equationEntity;
    }

    /***
     * 生成最多4项式的每位数不超过100的加减法
     * @return
     */
    public EquationVO createLel2(){
        EquationVO equationExamVO = new EquationVO();
        String equation = null;
        int digitNum = RandomUtil.randomInt(1,4);
        int ans = RandomUtil.randomInt(101);
        equation = ans+"";
        while(digitNum-- > 0){
            boolean isAdd = RandomUtil.randomBoolean();
            if(isAdd){
                int operand = RandomUtil.randomInt(101);
                equation += "+" + operand;
                ans += operand;
            }else{
                int operand = RandomUtil.randomInt(ans+1);
                equation += "-" + operand;
                ans -= operand;
            }
        }
        equationExamVO.setEquation(equation);
        equationExamVO.setLevel(2);
        equationExamVO.setType(1);
        return equationExamVO;
    }


    /***
     * 生成100以内的加减乘除法
     * @return
     */
    public EquationVO createLel3(){
        EquationVO equationExamVO = new EquationVO();
        String equation = null;
        boolean isMul = RandomUtil.randomBoolean();
        if(isMul){
            //是乘除法
            isMul = RandomUtil.randomBoolean();
            if(isMul){
                //是乘法
                equation = RandomUtil.randomInt(1,10)+"*"+RandomUtil.randomInt(1,10);
            }else{
                //除法
                int l = RandomUtil.randomInt(1,10);
                equation = l+"/";
                int r = 1;
                for(int i = 2; i <= l; i++){
                    if(l % i == 0){
                        boolean choose = RandomUtil.randomBoolean();
                        if(choose){
                            r = i;
                            break;
                        }
                    }
                }
                equation+=r;
            }
        }else{
            int digitNum = RandomUtil.randomInt(1,4);
            int ans = RandomUtil.randomInt(101);
            equation = ans+"";
            while(digitNum-- > 0){
                boolean isAdd = RandomUtil.randomBoolean();
                if(isAdd){
                    int operand = RandomUtil.randomInt(101);
                    equation += "+" + operand;
                    ans += operand;
                }else{
                    int operand = RandomUtil.randomInt(101);
                    equation += "-" + operand;
                    ans -= operand;
                }
            }
        }
        equationExamVO.setEquation(equation);
        equationExamVO.setLevel(3);
        equationExamVO.setType(1);
        return equationExamVO;
    }


    /***
     * 生成最多四项式的整数乘除法
     * @return
     */
    public EquationVO createLel4(){
        JEP jep = new JEP();
        EquationVO equationExamVO = new EquationVO();
        String equation = "";
        int operandNum = RandomUtil.randomInt(1,4);
        equation = RandomUtil.randomInt(101)+"";
        while(operandNum-- > 0){
            char operator = RandomUtil.randomChar("/*");
            if(operator == '*'){
                equation += "*" + RandomUtil.randomInt(101);
            }else{
                //标记是否成功生成操作数
                boolean flag = false;
                jep.parseExpression(equation);
                int value = (int) jep.getValue();
                for(int i = 2; i <= Math.sqrt(value); i++){
                    if(value % i == 0 && RandomUtil.randomBoolean()){
                        equation += "/"+i;
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    //没有生成操作数
                    equation += "/"+1;
                }
            }
        }
        equationExamVO.setEquation(equation);
        equationExamVO.setLevel(4);
        equationExamVO.setType(1);
        return equationExamVO;
    }

    /***
     * 生成操作数为100以内的乘除法，可能出现小数，有且仅有一个括号
     * @return
     */
    public EquationVO createLel5(){
        EquationVO equationExamVO = new EquationVO();
        String equation = "";
        int operandNum = RandomUtil.randomInt(2,5);
        //用列表存储操作数，方便加括号运算
        List<Integer>operands = new ArrayList<>();
        for(int i = 1; i <= operandNum; i++){
            operands.add(RandomUtil.randomInt(1,101));
        }
        //添加括号的位置,最多给两个操作数添加括号
        int pos = RandomUtil.randomInt(operandNum-1);
        for(int i = 0; i < operands.size(); i++){
            if(i > 0)equation += RandomUtil.randomChar("/*");
            if(i == pos){
                //添加左括号
                equation += "(";
            }
            equation += operands.get(i);
            if(i == pos+1){
                //添加右括号
                equation += ")";
            }
        }
        equationExamVO.setEquation(equation);
        equationExamVO.setLevel(5);
        equationExamVO.setType(1);
        return equationExamVO;
    }


    /***
     * 生成带括号解方程， 形式为: ax+b=c, 其中b或者c可以是简单的乘除算式
     * @return
     */
    public EquationVO createLel6(){
        EquationVO equationExamVO = new EquationVO();
        String equation = "";
        //生成系数
        int a = RandomUtil.randomInt(1,101);
        String b = createLel3().getEquation();
        String c = createLel3().getEquation();
        equation = a+"*x+"+b+"="+c;

        //设置entity属性
        equationExamVO.setEquation(equation);
        equationExamVO.setLevel(6);
        equationExamVO.setType(2);
        return equationExamVO;
    }



}
